﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace btnet.pages
{
    public partial class svn_view_revisions : CommonPage
    {
        protected int bugid;

        protected void Page_Load(object sender, EventArgs e)
        {
            Bug_Util.do_not_cache(Response);

            security = new Security();
            security.check_security(HttpContext.Current, Security.ANY_USER_OK);

            bugid = Convert.ToInt32(Bug_Util.sanitize_integer(Request["id"]));

            int permission_level = Bug.get_bug_permission_level(bugid, security);
            if (permission_level == Security.PERMISSION_NONE)
            {
                Response.Write("You are not allowed to view this item");
                Response.End();
            }

            titl.Text = Bug_Util.get_setting("AppTitle", "BugTracker.NET") + " - "
                + "view svn file revisions";


            string sql = @"
select
svnrev_revision [revision],
svnrev_repository [repository],
svnap_action [action],
svnap_path [file],
svnrev_author [user],
svnrev_svn_date [revision date],
replace(substring(svnrev_msg,1,4000),char(13),'<br>') [msg],

case when svnap_action not like '%D%' and svnap_action not like 'A%' then
	'<a target=_blank href=svn_diff.aspx?id=' + convert(varchar,svnap_id) + '>diff</a>'
	else
	''
end [view<br>diff],

case when svnap_action not like '%D%' then
'<a target=_blank href=svn_log.aspx?id=' + convert(varchar,svnap_id) + '>history</a>'
	else
	''
end [view<br>history<br>(svn log)]";

            //	if (websvn_url != "")
            //	{
            //		sql += ",\n '<a target=_blank href=\"" + websvn_url + "\">WebSvn</a>' [WebSvn<br>URL]";
            //		sql = sql.Replace("$PATH","' + svnap_path + '");
            //		sql = sql.Replace("$REV", "' + convert(varchar,svnrev_revision) + '");
            //	}

            sql += @"
		from svn_revisions
		inner join svn_affected_paths on svnap_svnrev_id = svnrev_id
		where svnrev_bug = $bg
		order by svnrev_revision desc, svnap_path";

            sql = sql.Replace("$bg", Convert.ToString(bugid));

            ds = DbUtil.get_dataset(sql);
        }

        /// <summary>
        /// 
        /// </summary>
        protected void DisplayList()
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                SortableHtmlTable.create_from_dataset(
                    Response, ds, "", "", false);

            }
            else
            {
                Response.Write("No revisions.");
            }
        }

    }
}
